package demo_10_07_01;

import java.util.Scanner;

/**
 * @author:JAVASM
 * @className:BuyGoods
 * @description:
 * @date:2021/10/7 20:29
 * @version:0.1
 * @since:1.8
 */
public class BuyGoods {

    private static Goods[] goods;

    static {
        goods = new Goods[3];
        goods[0] = new Goods(1, "笔记本", 10);
        goods[1] = new Goods(2, "笔    ", 15);
        goods[2] = new Goods(3, "手机  ", 5);
    }

    public void goodsList() {
        System.out.println("商品序号\t商品名称\t商品库存");
        for (Goods good : goods) {
            System.out.println(good.getId() + "\t\t" + good.getName() + "\t " + good.getCount());
        }
    }


    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        BuyGoods buyGoods = new BuyGoods();
        buyGoods.goodsList();

        System.out.println("请输入需要购买的商品序号：");
        int choice = input.nextInt();
        boolean bo = false;


        for (int i = 0; i < goods.length; i++) {

            if (goods[i].getId()==choice) {
                System.out.println("请输入需要购买的商品数量：");
                int num = input.nextInt();
                if (num>goods[i].getCount()){
                    throw new GoodsException("商品库存不足，请重新选择！");
                }
                if (goods[i].getCount() ==0){
                    throw new GoodsException("商品已售空，请重新选择！");
                }
                if (num<goods[i].getCount()){
                    goods[i].setCount(goods[i].getCount()-num);
                }
                buyGoods.goodsList();
                bo = true;
            }
        }

        if(bo==false)
        {
            throw new GoodsException("未找到");
        }
//
//        for (Goods good : goods) {
//            if (good.getId()!=choice){
//                throw new GoodsException("找不到该商品，请输入正确的商品序号！");
//
//            }
//            if (good.getId()==choice) {
//                System.out.println("请输入需要购买的商品数量：");
//                int num = input.nextInt();
//                if (num>good.getCount()){
//                    throw new GoodsException("商品库存不足，请重新选择！");
//                }
//                if (good.getCount() ==0){
//                    throw new GoodsException("商品已售空，请重新选择！");
//                }
//                if (num<good.getCount()){
//                    good.setCount(good.getCount()-num);
//                }
//                buyGoods.goodsList();
//            }
//
//        }




    }


}
